package com.example.shushiyunsst.fragment.main.wallet.sub

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.alibaba.fastjson.parser.Feature
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.LazyFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.db.User
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_BALANCE_INFO
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.fragment.main.wallet.bean.WalletHeadBean
import com.example.wallet.bean.BalanceInfoBean
import com.example.wallet.db.BalanceInfoManager
import ikidou.reflect.TypeBuilder

class WalletHeadFragment : LazyFragment() {

    companion object {
        fun newInstance(): WalletHeadFragment {
            val args = Bundle()

            val fragment = WalletHeadFragment()
            fragment.arguments = args
            return fragment
        }

        fun newInstance(mBean: WalletHeadBean): WalletHeadFragment {
            val args = Bundle()
            args.putSerializable("mBean", mBean)
            val fragment = WalletHeadFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var visibleTv: ImageView

    private lateinit var usdTv: TextView

    private lateinit var cnyTv: TextView

    private lateinit var titleTv: TextView

    private var data: BalanceInfoBean? = null

    private var mBean: WalletHeadBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        val serializable = arguments?.getSerializable("mBean")
        if (serializable != null) {
            mBean = serializable as WalletHeadBean
        }

        data = BalanceInfoManager.mBalanceInfoManager.getBalanceInfoBean()
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun lazyInit() {


        visibleTv.performClick()

    }

    @SuppressLint("SetTextI18n")
    override fun initView(view: View) {
        visibleTv = view.findViewById(R.id.visibleTv)
        usdTv = view.findViewById(R.id.usdTv)
        cnyTv = view.findViewById(R.id.cnyTv)
        titleTv = view.findViewById(R.id.titleTv)
        if (mBean != null) {
            titleTv.text = "${mBean?.tokenType}-${_mActivity.getString(R.string.all_capture)}"
        }
        visibleTv.setOnClickListener {
            visibleTv.isSelected = !visibleTv.isSelected
            toggleVisible()
        }
    }

    private fun toggleVisible() {
        if (data == null) {
            getUserBalanceInfo(_mActivity)
        }

        val usdTvStr = if (mBean == null) {
            data?.sstBalanceStr()
        } else {
            mBean?.getUSDString()
        }

        val cnyTvStr = if (mBean == null) {
            data?.cnyBalanceStr()
        } else {
            mBean?.getCNYString()
        }

        if (visibleTv.isSelected) {
            usdTv.text = usdTvStr

            cnyTv.text = cnyTvStr
        } else {

            var str0 = ""
            usdTvStr?.forEach { _ ->
                str0 += "*"
            }
            usdTv.text = str0

            var str1 = ""
            cnyTvStr?.forEach { _ ->
                str1 += "*"
            }
            cnyTv.text = str1
        }
    }

    override fun getLayoutId() = R.layout.wallet_head_fragment

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        if (msg?.message == TO_REFRESH_BALANCE_INFO) {
//            data = msg.obj as BalanceInfoBean
            data = BalanceInfoManager.mBalanceInfoManager.getBalanceInfoBean()
            toggleVisible()
        }
    }
}